package org.ht.demo.openapi.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import org.ht.demo.openapi.bean.MapBean;
import org.ht.demo.openapi.util.CodeuTIL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.math.BigInteger;


/**
 * udp
 * @author ht
 *
 */
@Component
@ChannelHandler.Sharable
public class UDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

	@Override
	protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
		//获取消息内容
		ByteBuf content = datagramPacket.content();
		//写到bytes中
		int length = content.readableBytes();
		byte[] bytes = new byte[length];
		content.readBytes(bytes);
		BigInteger bigInteger = new BigInteger(1, bytes);
		String data = bigInteger.toString(16);

		MapBean bmap= CodeuTIL.buileMdUdp(data);
		System.out.println("---------"+bmap.toString());
		DatagramPacket datagramPacket1 = new DatagramPacket(CodeuTIL.getbackdata(bmap),datagramPacket.sender());
		channelHandlerContext.channel().writeAndFlush(datagramPacket1);
		// 队列处理消息

	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		super.channelActive(ctx);
	}

}
